package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.mainUI.MainActivity;


/**
 * ---启动页
 */
public class StartActivity extends BaseActivity implements View.OnClickListener {
    private static final int END_JUMP = 10;
    private ImageView iv_start;
    private TextView tv_second;
    private int time = 0;
    private Handler myHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    time--;
                    if (time < 0) {
                        time = 0;
                    }
                    tv_second.setText(time + "s");
                    myHandler.sendEmptyMessageDelayed(1, 1000);
                    break;
                case END_JUMP:
                    Intent intent = new Intent(StartActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;

            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        tv_second = (TextView) findViewById(R.id.tv_second);
        findViewById(R.id.rl_jump).setOnClickListener(this);
        upload();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    private static final String TAG = "StartActivity";

    private void upload() {
        time = 3;
        myHandler.sendEmptyMessage(1);
        myHandler.sendEmptyMessageDelayed(END_JUMP, 3000);
    }

    @Override
    protected void onDestroy() {
        if (myHandler != null) {
            Log.e("---", "--cnm-");
            myHandler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_jump:
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
                if (myHandler != null) {
                    myHandler.removeCallbacks(null);
                }
                finish();
                break;

            default:
                break;
        }
    }
}
